//
//  ANDatePickerViewController.m
//  ShenWeiRent
//
//  Created by iMac on 2019/3/26.
//  Copyright © 2019年 liyefu. All rights reserved.
//

#import "ANDatePickerViewController.h"
#import "NSDate+Anna.h"

@interface ANDatePickerViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>

@property (nonatomic, strong) UIPickerView *picker;

@property (nonatomic, strong) NSArray *month;
@property (nonatomic, strong) NSArray *year;
@property (nonatomic, strong) NSArray *day;
@property (nonatomic, strong) NSArray *hour;

@end

@implementation ANDatePickerViewController

- (void)dealloc
{
    NSLog(@"%@销毁了",NSStringFromClass([self class]));
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setupSubViews];
    [self setupSubViews];
    
//    NSDateComponents *com = [[NSDateComponents alloc] init];
//    com.year = 2019;
//    com.month = 1;
//    NSDate *data = [[NSCalendar currentCalendar] dateFromComponents:com];
//    self.year = [[NSDate date] monthPickerDataSourceFromStartDate:data];
//    self.month = self.year.firstObject;
//    [self.picker reloadAllComponents];
    
    self.year = [[NSDate date] todayBetweenEndDateWithMonths:3];
    self.month = self.year.firstObject;
    self.day = self.month.firstObject;
    self.hour = @[@"00",@"01",@"02",@"03",@"04",@"05",@"06",@"07",@"08",@"09",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17",@"18",@"19",@"20",@"21",@"22",@"23"];
    [self.picker reloadAllComponents];
}

- (CGFloat)controllerHeight
{
    return [UIScreen mainScreen].bounds.size.height;
}

#pragma mark - UIPickerViewDataSource

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 5;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (component == 0) {
        return self.year.count;
    }
    if (component == 1) {
        return self.month.count;
    }
    if (component == 2) {
        return self.day.count;
    }
    if (component == 3) {
        return self.hour.count;
    }
    return 60;
}

#pragma mark - UIPickerViewDelegate

- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (component == 0) {
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        dateFormatter.dateFormat = @"yyyy年";
        NSArray *month = self.year[row];
        NSArray *day = month.firstObject;
        NSDate *date = day.firstObject;
        return [dateFormatter stringFromDate:date];
    }
    else if (component == 1) {
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        dateFormatter.dateFormat = @"MM月";
        NSArray *day = self.month[row];
        NSDate *date = day.firstObject;
        return [dateFormatter stringFromDate:date];
    }
    else if (component == 2) {
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        dateFormatter.dateFormat = @"dd日";
        NSDate *date = self.day[row];
        return [dateFormatter stringFromDate:date];
    }
    else if (component == 3) {
        return self.hour[row];
    }
    return [NSString stringWithFormat:@"%ld",row];
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UILabel *lab = (UILabel *)view;
    if (!lab) {
        lab = [[UILabel alloc] init];
        lab.adjustsFontSizeToFitWidth = YES;
        lab.textAlignment = NSTextAlignmentCenter;
        lab.backgroundColor = [UIColor clearColor];
        lab.font = [UIFont systemFontOfSize:12];
        lab.textColor = kColor333333;
    }
    lab.text = [self pickerView:pickerView titleForRow:row forComponent:component];
    return lab;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (component == 0) {
        self.month = self.year[row];
        [pickerView reloadComponent:1];
    }
    if (component == 1) {
        self.day = self.month[row];
        [pickerView reloadComponent:2];
    }
}

- (void)setupSubViews
{
    CGFloat contentHeight = 220 + kTabBarHeight - 49;
    
    UIView *contentView = [[UIView alloc] init];
    contentView.backgroundColor  = [UIColor whiteColor];
    [self.view addSubview:contentView];
    
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.height.equalTo(@(contentHeight));
    }];
    
    
    UIButton *cancelBtn = [[UIButton alloc ] init];
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    cancelBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [cancelBtn setTitleColor:ThemeColor forState:UIControlStateNormal];
    [cancelBtn addTarget:self action:@selector(cancelAction:) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:cancelBtn];
    
    [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(contentView);
        make.top.equalTo(contentView);
        make.width.equalTo(@50);
        make.height.equalTo(@40);
    }];
    
    UIButton *sureBtn = [[UIButton alloc ] init];
    [sureBtn setTitle:@"确定" forState:UIControlStateNormal];
    sureBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [sureBtn setTitleColor:ThemeColor forState:UIControlStateNormal];
    [sureBtn addTarget:self action:@selector(sureAction:) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:sureBtn];
    
    [sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(contentView);
        make.right.equalTo(contentView);
        make.width.equalTo(@50);
        make.height.equalTo(@40);
    }];
    
    UIView *line = [[UIView alloc] init];
    line.backgroundColor = RGBAColor(106, 106, 106, 0.16);
    [contentView addSubview:line];
    
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(contentView);
        make.top.equalTo(sureBtn.mas_bottom);
        make.height.equalTo(@1);
    }];
    
    self.picker = [[UIPickerView alloc] init];
    self.picker.dataSource = self;
    self.picker.delegate = self;
    [contentView addSubview:self.picker];
    
    [self.picker mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(contentView);
        make.top.equalTo(line.mas_bottom);
        make.bottom.equalTo(@(kTabBarHeight - 49));
    }];
    
}

- (void)cancelAction:(id)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)sureAction:(id)sender
{
    if (self.selectDate) {
        NSDate *date = self.day[[self.picker selectedRowInComponent:2]];
        NSDateComponents *com = [[NSCalendar currentCalendar] components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:date];
        com.hour = [self.picker selectedRowInComponent:3];
        com.minute  = [self.picker selectedRowInComponent:4];
        date = [[NSCalendar currentCalendar] dateFromComponents:com];
        self.selectDate(date);
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end
